<?php
//global variable
$alexa_backlink=0;
$alexa_reach=0;
$techno_inblogs=0;
$techno_inlinks=0;
$techno_update='';

//--> for google pagerank
function StrToNum($Str, $Check, $Magic)
{
    $Int32Unit = 4294967296;  // 2^32

    $length = strlen($Str);
    for ($i = 0; $i < $length; $i++) {
        $Check *= $Magic;     
        //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
        //  the result of converting to integer is undefined
        //  refer to http://www.php.net/manual/en/language.types.integer.php
        if ($Check >= $Int32Unit) {
            $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
            //if the check less than -2^31
            $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
        }
        $Check += ord($Str{$i});
    }
    return $Check;
}

//--> for google pagerank
/*
* Genearate a hash for a url
*/
function HashURL($String)
{
    $Check1 = StrToNum($String, 0x1505, 0x21);
    $Check2 = StrToNum($String, 0, 0x1003F);

    $Check1 >>= 2;     
    $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
    $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
    $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);   
   
    $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
    $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
   
    return ($T1 | $T2);
}

//--> for google pagerank
/*
* genearate a checksum for the hash string
*/
function CheckHash($Hashnum)
{
    $CheckByte = 0;
    $Flag = 0;

    $HashStr = sprintf('%u', $Hashnum) ;
    $length = strlen($HashStr);
   
    for ($i = $length - 1;  $i >= 0;  $i --) {
        $Re = $HashStr{$i};
        if (1 === ($Flag % 2)) {             
            $Re += $Re;     
            $Re = (int)($Re / 10) + ($Re % 10);
        }
        $CheckByte += $Re;
        $Flag ++;   
    }

    $CheckByte %= 10;
    if (0 !== $CheckByte) {
        $CheckByte = 10 - $CheckByte;
        if (1 === ($Flag % 2) ) {
            if (1 === ($CheckByte % 2)) {
                $CheckByte += 9;
            }
            $CheckByte >>= 1;
        }
    }

    return '7'.$CheckByte.$HashStr;
}

//get google pagerank
function getpagerank($url) {
    $query="http://toolbarqueries.google.com/search?client=navclient-auto&ch=".CheckHash(HashURL($url)). "&features=Rank&q=info:".$url."&num=100&filter=0";
    $data=file_get_contents_curl($query);
    //print_r($data);
    $pos = strpos($data, "Rank_");
    if($pos === false){} else{
        $pagerank = substr($data, $pos + 9);
        return $pagerank;
    }
}


//get technorati rank
function get_technorati_rank($url, $apikey)
{
    global $techno_url, $techno_inblogs, $techno_inlinks, $techno_update;
   
    $technorati_xml = "http://api.technorati.com/bloginfo?key=" . $apikey . "&url=" . $url;
    $xml_parser = xml_parser_create();
    /*
    $fp = fopen($technorati_xml, "r") or die("Error: Reading XML data.");
    $data = "";
    while (!feof($fp)) {
        $data .= fread($fp, 8192);
    }
    fclose($fp);
    */
    $data=file_get_contents_curl($technorati_xml);
    xml_parse_into_struct($xml_parser, $data, $vals, $index);
    xml_parser_free($xml_parser);
   
    //get values
    $index_rank = $index['RANK'][0];
    $techno_rank = $vals[$index_rank]['value'];
    //print_r($vals);

    $index_inblogs = $index['INBOUNDBLOGS'][0];
    $techno_inblogs = number_format(trim($vals[$index_inblogs]['value']));
    $index_inlinks = $index['INBOUNDLINKS'][0];
    $techno_inlinks = number_format(trim($vals[$index_inlinks]['value']));
    $index_update = $index['LASTUPDATE'][0];
    $techno_update = trim($vals[$index_update]['value']);

   
    return $techno_rank;
}

//get alexa popularity
function get_alexa_popularity($url)
{   
global $alexa_backlink, $alexa_reach;
    $alexaxml = "http://xml.alexa.com/data?cli=10&dat=nsa&url=".$url;
   
    $xml_parser = xml_parser_create();
    /*
    $fp = fopen($alexaxml, "r") or die("Error: Reading XML data.");
    $data = "";
    while (!feof($fp)) {
        $data .= fread($fp, 8192);
        //echo "masuk while<br />";
    }
    fclose($fp);
    */
    $data=file_get_contents_curl($alexaxml);
    xml_parse_into_struct($xml_parser, $data, $vals, $index);
    xml_parser_free($xml_parser);
   
    //print_r($vals);
    //echo "<br />";
    //print_r($index);
   
    $index_popularity = $index['POPULARITY'][0];
    $index_reach = $index['REACH'][0];
    $index_linksin = $index['LINKSIN'][0];
    //echo $index_popularity."<br />";
    //print_r($vals[$index_popularity]);
    $alexarank = $vals[$index_popularity]['attributes']['TEXT'];
    $alexa_backlink = $vals[$index_linksin]['attributes']['NUM'];
    $alexa_reach = $vals[$index_reach]['attributes']['RANK'];
   
    return $alexarank;
}

//get alexa backlink
function alexa_backlink($url)
{
    global $alexa_backlink;
    if ($alexa_backlink!=0)
    {
        return $alexa_backlink;
    } else {
        $rank=get_alexa_popularity($url);
        return $alexa_backlink;
    }
}

//get alexa reach rank
function alexa_reach_rank($url)
{
    global $alexa_reach;
    if ($alexa_reach!=0)
    {
        return $alexa_reach;
    } else {
        $rank=get_alexa_popularity($url);
        return $alexa_reach;
    }
}

//get exactrank (from exactrank.com)
function get_exactrank($url)
{
    $exactrankurl = "http://exactrank.com/index.php?url=".urlencode($url);
    $data = file_get_contents_curl($exactrankurl);
    $spl=explode('<td style="text-align:right;">',$data);
    //print_r($spl[1]);
    $spl2=explode('</span>',$spl[1]);
    $ret=trim($spl2[0]);
    if(strlen($ret)==0)
    {
        return(0);
    }
    else
    {
        return($ret);
    }
}


//get google backlink
function google_backlink($uri)
{
    $uri = trim(eregi_replace('http://', '', $uri)); $uri = trim(eregi_replace('http', '', $uri));
    $url = 'http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=link:'.$uri.'&filter=0';
    $v = file_get_contents_curl($url);
    preg_match('/of about \<b\>(.*?)\<\/b\>/si',$v,$r);
    preg_match('/of \<b\>(.*?)\<\/b\>/si',$v,$s);
    if ($s[1]!=0) {
        return $s[1];
    } else {
        return ($r[1]) ? $r[1] : '0';
    }
}

//get yahoo inlink/backlink
function yahoo_inlink($uri)
{
    $uri = trim(eregi_replace('http://', '', $uri)); $uri = trim(eregi_replace('http', '', $uri));
    $url = 'http://siteexplorer.search.yahoo.com/advsearch?p=http://'.$uri.'&bwm=i&bwmf=s&bwmo=&fr2=seo-rd-se';
    $v = file_get_contents_curl($url);
    preg_match('/of about \<strong\>(.*?) \<\/strong\>/si',$v,$r);
    return ($r[1]) ? $r[1] : '0';
}

//get altavista search result count
function altavista_link($sURL)
{
    $url="http://www.altavista.com/web/results?itag=ody&q=link%3A$sURL&kgs=0&kls=0";
    $data = file_get_contents_curl($url);
    $spl=explode("AltaVista found ",$data);
    $spl2=explode(" results",$spl[1]);
    $ret=trim($spl2[0]);
    if(strlen($ret)==0)
    {
        return(0);
    }
    else
    {
        return($ret);
    }
   
}

//get alltheweb search result count
function alltheweb_link($sURL)
{
    $url="http://www.alltheweb.com/search?cat=web&cs=utf-8&q=link%3A".urlencode($sURL)."&_sb_lang=any";
    $data = file_get_contents_curl($url);
    $spl=explode("</span> of <span class=\"ofSoMany\">",$data);
    $spl2=explode("</span>",$spl[1]);
    $ret=trim($spl2[0]);
    if(strlen($ret)==0)
    {
        return(0);
    }
    else
    {
        return($ret);
    }
}

//get google indexed page
function google_indexed($uri)
{
    $uri = trim(eregi_replace('http://', '', $uri)); $uri = trim(eregi_replace('http', '', $uri));
    $url = 'https://encrypted.google.com/search?hl=en&lr=&ie=UTF-8&q=site:'.$uri.'&filter=0';
    $v = file_get_contents_curl($url);
    preg_match('/About \<b\>(.*?)\<\/b\>/si',$v,$r);
    print_r($v);
    die;
    
    preg_match('/of \<b\>(.*?)\<\/b\>/si',$v,$s);
    
    if ($s[1]!=0) {
        return $s[1];
    } else {
        return ($r[1]) ? $r[1] : '0';
    }
}

//get yahoo indexed page
function yahoo_indexed($uri)
{
    $uri = trim(eregi_replace('http://', '', $uri)); $uri = trim(eregi_replace('http', '', $uri));
    $url = 'http://siteexplorer.search.yahoo.com/advsearch?p=http://'.$uri.'&bwm=p&bwmf=s&bwmo=d';
    $v = file_get_contents_curl($url);
    preg_match('/of about \<strong\>(.*?) \<\/strong\>/si',$v,$r);
    return ($r[1]) ? $r[1] : '0';
}

//get msn indexed page
function msn_indexed($uri)
{
    $uri = trim(eregi_replace('http://', '', $uri)); $uri = trim(eregi_replace('http', '', $uri));
    $url = 'http://search.msn.com/results.aspx?q=site:'.$uri;
    $data = file_get_contents_curl($url);
    $spl=explode("of",$data);
    $spl2=explode("results",$spl[1]);
    $ret=trim($spl2[0]);
    if(strlen($ret)==0)
    {
        return(0);
    }
    else
    {
        return($ret);
    }
}

//get googlebot last access
function googlebot_lastaccess($url)
{
    $url = 'http://209.85.175.104/search?hl=en&q=cache:'.$url.'&btnG=Google+Search&meta=';
    $data = file_get_contents_curl($url);
    $spl=explode("as retrieved on",$data);
    //echo "<pre>".$spl[0]."</pre>";
    $spl2=explode(".<br>",$spl[1]);
    $ret=trim($spl2[0]);
    //echo "<pre>".$spl2[0]."</pre>";
    if(strlen($ret)==0)
    {
        return(0);
    }
    else
    {
        return($ret);
    }
}

//get blogworth
function blogworth($url)
{
    $worthurl='http://www.business-opportunities.biz/projects/how-much-is-your-blog-worth/submit/';
    $data=array('url'=>$url);
    $data=http_build_query($data);
   
    $res=do_post_request_curl($worthurl, $data);
    $spl=explode("is worth",$res);
    $spl2=explode("</p>",$spl[1]);
    $ret=trim($spl2[0]);
    if(strlen($ret)==0)
    {
        return(0);
    }
    else
    {
        return($ret);
    }
}

//for POST request with curl
function do_post_request_curl($url, $data)
{
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); // set url to post to 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable 
    curl_setopt($ch, CURLOPT_POST, 1); // set POST method 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // add POST fields 
    $result = curl_exec($ch); // run the whole process 
    //echo $result;
    curl_close($ch);   
    return $result; 
}

//function to check whether an url is listed in DMOZ(ODP), return 1 or 0
function dmoz_listed($url)
{
    $url = trim(eregi_replace('http://', '', $url));
    $url = trim(eregi_replace('http', '', $url));
    $dmozurl='http://search.dmoz.org/cgi-bin/search?search='.$url;
    $data = file_get_contents_curl($dmozurl);
    //echo "<pre>".$data."</pre>";
    $pos=strpos($data, 'match');
    if ($pos==0) {
        return 0;
    } else {
        return 1;
    }
}

function file_get_contents_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Pagerank lib test page</title>
</head>

<body>
<form id="form1" name="form1" method="get" action="">
  URL :
  <input name="url" type="text" id="url" value="<?php echo $_REQUEST['url']; ?>" />
  <input type="submit" name="Submit" value="Submit" />
</form>
<?php
if ((isset($_REQUEST['Submit'])) && ($_REQUEST['url']!='')) {
?>
<fieldset>
<legend>Query result</legend>
<?php
$url=$_REQUEST['url'];

echo "Technorati rank = ".get_technorati_rank($url, "ee470f3fa8571bc7bc83eddaa35ae153")."<br />";
echo "Technorati inblogs(authority) = ".$techno_inblogs."<br />";
echo "Technorati inlinks = ".$techno_inlinks."<br />";
echo "Technorati last update = ".$techno_update."<br />";
echo "Google pagerank = ".getpagerank($url)."<br />";
echo "Alexa popularity = ".get_alexa_popularity($url)."<br />";
echo "Alexa backlink = ".alexa_backlink($url)."<br />";
echo "Google backlink = ".google_backlink($url)."<br />";
echo "Yahoo inlink = ".yahoo_inlink($url)."<br />";
echo "Google indexed = ".google_indexed($url)."<br />";
echo "Yahoo indexed = ".yahoo_indexed($url)."<br />";
echo "MSN indexed = ".msn_indexed($url)."<br />";
echo "AllTheWeb result = ".alltheweb_link($url)."<br />";
echo "Altavista result = ".altavista_link($url)."<br />";
echo "Exactrank = ".get_exactrank($url)."<br />";
echo "Googlebot last access = ".googlebot_lastaccess($url)."<br />";
echo "Blogworth = ".blogworth($url)."<br />";
echo "Listed in DMOZ ? ".dmoz_listed($url)."<br />";
?>
</fieldset>
<?php
};
?>
</body>
</html>